/**
 * @(#)com.framework.web.sys.service.UserService
 *
 * Copyright (c) 2014-2018 上海天跃科技有限公司
 *
 * DESC:
 *
 */
package com.framework.web.sys.service;

import com.framework.core.util.StringUtil;
import com.framework.web.sys.domain.Organization;
import com.framework.web.sys.domain.User;
import com.framework.web.sys.domain.User;
import com.framework.web.sys.domain.UserRole;
import com.framework.web.sys.repository.OrganizationRepository;
import com.framework.web.sys.repository.UserRepository;
import com.framework.web.sys.repository.UserRoleRepository;
import com.framework.web.sys.repository.specifications.UserSpecs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

/**
 * @author fusf
 * @version 1.0  2016/1/8
 */
@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private OrganizationRepository organizationRepository;


    /**
     * 分页查找
     *
     * @param user
     * @param pr
     * @return
     */
    public Page<User> getPageList(User user, PageRequest pr) {

        if (user.getOrgId() != null) { //组织机构信息
            Organization organization = organizationRepository.findOne(user.getOrgId());
            user.setOrganization(organization);
        }
        Page<User> totalList = userRepository.findAll(
                UserSpecs.LikeUserName(user), pr);
        return totalList;
    }

    /**
     * 新增或者保存
     *
     * @param user
     * @return
     */
    public User save(User user) {
        return userRepository.save(user);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void delete(String id) {
        userRepository.delete(id);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    public User findById(String id) {
        return userRepository.findOne(id);
    }





}
